﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace Innova.Microfinanzas.Business.UsersAndSecurity
{
    public class EmployeeManager
    {
        private MyCommerceEntities vContext;

        public EmployeeManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public EmployeeManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<Employee> GetEmployeeActives()
        {
            List<Employee> colEmployees = new List<Employee>();

            var result = from em in vContext.Employee
                         .Include("DealerStore")
                         where em.IsActive == true
                         select em;
            if (result != null)
                colEmployees = new List<Employee>(result.AsEnumerable<Employee>());

            return colEmployees;
        }

        public void CreateEmployee(Employee objEmployee)
        {
            vContext.AddObject("Employee", objEmployee);
            vContext.SaveChanges();
        }

        public void UpdateEmployee(Employee objEmployee)
        {
            var vResult = (from us in vContext.Employee
                           where us.IdEmployee == objEmployee.IdEmployee
                           select us).First();

            vResult.IdEmployee = objEmployee.IdEmployee;
            vResult.FamilyName1 = objEmployee.FamilyName1;
            vResult.FamilyName2 = objEmployee.FamilyName2;
            vResult.IdDealerStore = objEmployee.IdDealerStore;
            vResult.IsActive = objEmployee.IsActive;
            vResult.Name = objEmployee.Name;

            vContext.SaveChanges();
        }

        public void DeleteEmployee(Employee objEmployee)
        {
            var vResult = (from us in vContext.Employee
                           where us.IdEmployee == objEmployee.IdEmployee
                           select us).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }
    }
}
